package top.javarem.medical.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import top.javarem.medical.common.response.Response;
import top.javarem.medical.domain.dto.*;
import top.javarem.medical.domain.po.User;
import top.javarem.medical.service.UserService;

import javax.annotation.Resource;
import java.util.List;



@Slf4j
@RestController
@RequestMapping("/api/v1/medical/user")
@CrossOrigin("*")
public class UserController {

    @Resource
    private UserService userService;


    //登录
    @PostMapping("/login")
    public Response<LoginResponseDTO> login(@RequestBody LoginDTO loginDTO) {

        log.info("用户登录：{}", loginDTO);

        LoginResponseDTO dto = userService.login(loginDTO);
        return Response.success(dto);
    }

    //注册
    @PostMapping("/register")
    public Response<Boolean> register(@RequestBody RegisterDTO registerDTO) {

        log.info("用户注册：{}", registerDTO);
        Boolean isRegister = userService.register(registerDTO);
        if (isRegister) {
            log.info("注册成功");
            return Response.success(true);
        } else {
            return Response.error(false);
        }

    }

    //查询所有用户
    @GetMapping("/query_all_user")
    public Response<List<UserDTO>> queryAllUser() {

        try {
            List<UserDTO> userDTOList = userService.queryAllUser();
            if (CollectionUtils.isEmpty(userDTOList)) {
                return Response.error();
            }
            return Response.success(userDTOList);
        } catch (Exception e) {
            return Response.error();
        }

    }


    //查询用户详情
    @PostMapping("/query_user_detail")
    public Response<UserDetailDTO> queryUserDetail(@RequestBody UpdateUserInfoDTO updateUserInfoDTO) {

        UserDetailDTO userDetailDTO = userService.queryUserDetail(updateUserInfoDTO);
        log.info("查询用户详情: {}", userDetailDTO.toString());
        return Response.success(userDetailDTO);

    }

    //更新用户信息
    @PostMapping("/update_user_info")
    public Response<Boolean> updateUser(@RequestBody UserDTO userDTO) {

        Boolean update = userService.updateUser(userDTO);
        if (update) {
            return Response.success(true);
        }
        return Response.error(false);

    }

    @PostMapping("/update_user")
    public Response<Boolean> updateUser(@RequestBody UpdateUserDTO updateUserInfoDTO) {
        Boolean update = userService.updateUserInfo(updateUserInfoDTO);
        if (update) {
            return Response.success(true);
        }
        return Response.error(false);
    }

    //更新用户状态
    @PostMapping("/update_user_status")
    public Response<Boolean> updateUserStatus(@RequestBody UpdateUserInfoDTO updateUserInfoDTO) {

        boolean update = userService.lambdaUpdate()
                .set(User::getStatus, updateUserInfoDTO.getStatus())
                .eq(User::getId, updateUserInfoDTO.getId())
                .update();
        if (update) {
            log.info("用户状态修改成功: {}", updateUserInfoDTO);
            return Response.success(true);
        }
        log.info("用户状态修改失败: {}", updateUserInfoDTO);
        return Response.error(false);



    }

}
